//
//  DetailViewController.swift
//  issac-note
//
//  Created by Issac on 16/8/21.
//  Copyright © 2016年 Issac. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON


//protocol SendNoticeDelegate{
//
//    func sendNotice(sender: Bool)
//
//}
class DetailViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
    
    @IBOutlet var titleText: UITextField!
    @IBOutlet var contentText: UITextView!
    @IBOutlet var navRightBtn: UIBarButtonItem!

    var noteTitle: String?
    var noteContent: String?
    var noteId: String?

    var indexPath: Int?
    var delegate: SendNoticeDelegate?
    var isUpdate: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        
        titleText.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 0))
        titleText.leftViewMode = UITextFieldViewMode.Always
        
        self.titleText.delegate = self
        self.contentText.delegate = self
        
        self.titleText.text = self.noteTitle
        self.contentText.text = self.noteContent
        
        let leftBarBtn = UIBarButtonItem(title: "返回", style: .Plain, target: self, action: #selector(backToList))
        self.navigationItem.leftBarButtonItem = leftBarBtn
        
    }
    
    
    func backToList(){
    
        if self.delegate != nil {
        
            self.delegate?.sendNotice(self.isUpdate)
            self.navigationController?.popViewControllerAnimated(true)
        
        }
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //标题重新分配第一响应者
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        
        return true
        
    }
    
    //开始编辑内容触发的事件
    func textViewDidBeginEditing(textView: UITextView) {
        
        self.navRightBtn.title = "完成"
        
    }
    //MARK: - IBAction function
    //导航栏右按钮事件
    @IBAction func textViewEditingEnd(sender: UIBarButtonItem) {
        
        if(sender.title != " "){
        
            self.contentText.resignFirstResponder()
            self.titleText.resignFirstResponder()
            sender.title = " "
            
        }
        
    }
    
    @IBAction func syncNoteAction(sender: UIBarButtonItem) {
        
        self.syncNoteToServer()
        
    }
    
    //MARK: -syncNote
    
    func syncNoteToServer() {
    
        let url = "http://issac.website/issac-noteBackstage/Admin/Index/updateNote"
        let parameters = [
            "note_id" : self.noteId!,
            "title" : self.titleText.text,
            "ctt": self.contentText.text
        ]
        
        Alamofire.request(.POST, url, parameters: parameters).responseJSON { response in
            
            switch response.result {
            
            case .Success:
                
                if let value = response.result.value {
                
                    let json = JSON(value)
                    if(!json["status"]) {
                        
                        self.alertTipAction("同步成功")
                        self.isUpdate = true
                    
                    }else {
                    
                        self.alertTipAction("同步失败")
                    
                    }
                
                }
                
            case .Failure:
                
                self.alertTipAction("同步失败，请稍后再试")
            
            }
            
        }
        
    }
    
    //alertView
    func alertTipAction(message: String){
    
        let alertAction = UIAlertController(title: "日志详情", message: message, preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "确定", style: .Cancel, handler: nil)
        
        alertAction.addAction(cancelAction)
        self.presentViewController(alertAction, animated: true, completion: nil)
    
    }
    
    
    

}
